import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2024-09-18
 * Time: 20:27
 */
public class Test {
    public static void main(String[] args) {
        int[] array={1,4,7,21,45,81,34,51,42};
        //bubbleSort(array);
        quickSort(array,0,array.length-1);
        System.out.println(Arrays.toString(array));
    }
    public static void swap(int[] array,int i,int j){
        int tmp=array[i];
        array[i]=array[j];
        array[j]=tmp;
    }
    // 冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            for (int j = i+1; j < array.length; j++) {
                if(array[i]>array[j]){
                    swap(array,i,j);
                }
            }
        }
    }

// 快速
    public static void quickSort(int[] array,int left,int right){
        if((right-left)<=1){
            return;
        }
        int div =partition(array,left,right);
        quickSort(array,left,div-1);
        quickSort(array,div+1,right);
    }

    private static int partition(int[] array, int left, int right){
        int i=left;
        int j=right;
        while (i<j){
            while(i<j && array[j]>=array[left]){
                j--;
            }
            while (i<j && array[i]<=array[left]){
                i++;
            }
            swap(array,i,j);
        }
        swap(array,i,left);
        return i;
    }
}
